package com.kehutong.wxapi.service.impl;

import com.kehutong.wxapi.request.*;
import com.kehutong.wxapi.response.DepartmentCreateResponse;
import com.kehutong.wxapi.response.DepartmentDeleteResponse;
import com.kehutong.wxapi.response.DepartmentListResponse;
import com.kehutong.wxapi.service.DepartmentService;
import com.kehutong.wxapi.util.KhtClient;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSON;
import org.coraframework.json.JSONObject;

/**
 * 部门管理服务实现类
 * @author: liukunlong
 * @date: 2021-03-08 17:05
 */
public class DepartmentServiceImpl implements DepartmentService {
    @Inject
    private KhtClient khtClient;

    /**
     * 创建部门
     * 请求地址：https://qyapi.weixin.qq.com/cgi-bin/department/create?access_token=ACCESS_TOKEN
     * @param departmentCreateRequest 创建部门请求对象
     * @return 创建部门响应对象
     * @throws Exception 异常
     */
    public DepartmentCreateResponse departmentCreate(DepartmentCreateRequest departmentCreateRequest) throws Exception{
        JSONObject jsonObject = khtClient.get("/cgi-bin/department/create", http -> {
            http.addParam("access_token", departmentCreateRequest.getAccessToken());
            departmentCreateRequest.setAccessToken(null);
            http.setBody(JSON.toJSONString(departmentCreateRequest));
        });
        return JSON.parseObject(jsonObject.toJSONString(), DepartmentCreateResponse.class);
    }

    /**
     * 更新部门
     * 请求地址：https://qyapi.weixin.qq.com/cgi-bin/department/update?access_token=ACCESS_TOKEN
     * @param departmentUpdateRequest 更新部门请求对象
     * @return 更新部门响应对象
     * @throws Exception 异常
     */
    public DepartmentUpdateResponse departmentUpdate(DepartmentUpdateRequest departmentUpdateRequest) throws Exception{
        JSONObject jsonObject = khtClient.get("/cgi-bin/department/update", http -> {
            http.addParam("access_token", departmentUpdateRequest.getAccessToken());
            departmentUpdateRequest.setAccessToken(null);
            http.setBody(JSON.toJSONString(departmentUpdateRequest));
        });
        return JSON.parseObject(jsonObject.toJSONString(), DepartmentUpdateResponse.class);
    }


    /**
     * 删除部门
     * 请求地址：请求地址：https://qyapi.weixin.qq.com/cgi-bin/department/delete?access_token=ACCESS_TOKEN&id=ID
     * @param departmentDeleteRequest 删除部门请求对象
     * @return 删除部门响应对象
     * @throws Exception 异常
     */
    public DepartmentDeleteResponse departmentDelete(DepartmentDeleteRequest departmentDeleteRequest) throws Exception{
        JSONObject jsonObject = khtClient.get("/cgi-bin/department/delete", http -> {
            http.addParam("access_token", departmentDeleteRequest.getAccessToken());
            http.addParam("id", String.valueOf(departmentDeleteRequest.getId()));
        });
        return JSON.parseObject(jsonObject.toJSONString(), DepartmentDeleteResponse.class);
    }

    /**
     * 获取部门列表
     * 请求地址：https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=ACCESS_TOKEN&id=ID
     * @param departmentListRequest 获取部门列表请求对象
     * @return 获取部门列表响应对象
     * @throws Exception 异常
     */
    public DepartmentListResponse departmentList(DepartmentListRequest departmentListRequest) throws Exception{
        JSONObject jsonObject = khtClient.get("/cgi-bin/department/list", http -> {
            http.addParam("access_token", departmentListRequest.getAccessToken());
            http.addParam("id", String.valueOf(departmentListRequest.getId()));
        });
        return JSON.parseObject(jsonObject.toJSONString(), DepartmentListResponse.class);
    }
}
